package br.com.poliwar.view {
	import adobe.utils.CustomActions;
	import br.com.poliwar.model.vo.UsuarioVO;
	import br.com.poliwar.Sala;
	import br.com.poliwar.view.componentes.events.ItemSalaEvent;
	import br.com.poliwar.view.componentes.ItemSala;
	import br.com.poliwar.view.events.TelaBuscaSalaEvent;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Ricardo Teixeira
	 */
	public class TelaBuscaSala extends SalasSWC{
		private var conteiner:Sprite;
		private var itens:Vector.<ItemSala>;
		
		public function TelaBuscaSala() {			
			this.conteiner = new Sprite ();
			this.itens = new Vector.<ItemSala> ();
			
			for (var i:int = 0; i < 13; i++) {
				var sala:Sala = new Sala (i, new UsuarioVO (i, "Teix " + i), new Vector.<UsuarioVO> (), i + 2);
				this.adicionarSala (sala);
			}
			
			this.voltar.addEventListener (MouseEvent.CLICK, onClickVoltar);
			
			this.salas.mouseEnabled = true;
			this.salas.mouseChildren = true;
			this.salas.source = conteiner;
		}
		
		private function onClickVoltar(e:MouseEvent):void {
			var evento:TelaBuscaSalaEvent = new TelaBuscaSalaEvent (TelaBuscaSalaEvent.VOLTAR);
			this.dispatchEvent (evento);
		}
		
		public function adicionarSala (sala:Sala):void {
			var posY:Number = this.conteiner.height;
			var item:ItemSala = new ItemSala (sala);
			this.itens.push (item);
			item.addEventListener (ItemSalaEvent.CLICK_ITEM, onClickSala);
			item.x = 10;
			item.y = 10 + (this.conteiner.numChildren * (item.height + 10));
			this.conteiner.addChild (item);
		}
		
		public function removerSala (sala:Sala):void {
			var id:int = -1;
			
			for (var i:int = 0; i < this.conteiner.numChildren && id == -1; i++) {
				var itemTemp:ItemSala = this.conteiner.getChildAt (i) as ItemSala;
				if (itemTemp.sala == sala) {
					id = i;
				}
			}
			if (id != -1) {
				this.itens.splice (id, 1);	
				this.conteiner = new Sprite ();			
				for (i = 0; i < this.itens.length; i++) {
					var item:ItemSala = this.itens[i];
					item.x = 10;
					item.y = 10 + (this.conteiner.numChildren * (item.height + 10));
					this.conteiner.addChild (item);
				}
				this.salas.source = conteiner;
			}
		}
		
		private function onClickSala(e:ItemSalaEvent):void {
			var evento:TelaBuscaSalaEvent = new TelaBuscaSalaEvent (TelaBuscaSalaEvent.SELECIONA_SALA, e.sala);
			this.dispatchEvent (evento);
		}
		
	}

}